<?php

require_once dirname(__FILE__).'/../lib/domainGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/domainGeneratorHelper.class.php';

/**
 * domain actions.
 *
 * @package    wiki
 * @subpackage domain
 * @author     Elmaho
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class domainActions extends autoDomainActions
{
	
	public function executeIndex(sfWebRequest $request)
	{
		$this->redirect('domain/listArbo');
	}
	
	public function executeShow(sfWebRequest $request)
	{
		$this->domain = Doctrine_Core::getTable('Domain')->find(array($request->getParameter('id')));
    	$this->forward404Unless($this->domain);
	}
	
  public function executeNew(sfWebRequest $request)
  {
  	parent::executeNew($request);
  	
  	$parent = $request->getParameter("parent");
  	if($parent)
  		$this->form->setParent($parent);
  }
	
	/**
	 * Pour afficher la premiere page avec les arborescences
	 */
	public function executeListArbo(sfWebRequest $request){
		$resp = array();
		
		$this->root = $treeObject = Doctrine_Core::getTable('Domain')->getTree();
		
		sfProjectConfiguration::getActive()->loadHelpers('Url');
		
		foreach ($treeObject->fetchRoots() as $root) {
		  $resp[] =  $this->display_children($root);
		}
		
        $this->json = '('.json_encode($resp).')';
	}
	
	public function display_children(&$node){
		
		
		$url = url_for("domain/show?id=".$node->getId());
		$name = $node->getNameDom();
		
		$elem = array("id" =>$node->getId(), "title"=>$name, "url" => $url);
			
		$enfants = $node->getNode()->getChildren();
		$subs = array();
		
		if($enfants != null){
			foreach ($enfants as $fils){
				$subs[] = $this->display_children($fils);
			}
		}
		
		if(count($subs) > 0)
		{
			$elem['isFolder'] = 'true';
			$elem['children'] = $subs;
			$elem['url'] = url_for("domain/new?parent=".$node->getId());
		}
		
		return $elem;
	}
}
